import { createRouter, createWebHashHistory } from 'vue-router'
import { message } from 'ant-design-vue'
import { useLoginStore } from '@/stores'
import { createPinia } from 'pinia'

const { localStorage } = window
const pinia = createPinia()

const userStore = useLoginStore(pinia)

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home',
    },
    {
      path: '/home',
      name: 'home',
      meta: {
        title: '首页',
        auth: true
      },
      component: () => import('@/views/IndexView.vue'),
      redirect: '/home/crossStreet',
      children: [
        {
          path: '/home/crossStreet',
          name: 'crossStreet',
          meta: {
            title: '第一个项目',
          },
          component: () => import('@/views/crossStreet/index.vue')
        },
        {
          path: '/home/Fibonacci',
          name: 'Fibonacci',
          meta: {
            title: '斐波那契',
          },
          component: () => import('@/views/Fibonacci/index.vue')
        },
      ]
    }
  ]
})

router.beforeEach(async (to, from, next) => {
  // if (userStore.hasLogin) {
  //   next()
  // } else if (to.query && to.query.refreshToken) {
  //   localStorage.setItem('LOCAL_REFRESH_TOKEN_KEY', 'Bearer '+ to.query.refreshToken)
  //   // const loading = ElLoading.service({
  //   //   lock: true,
  //   //   text:'加载中...',
  //   //   background: '#fff',
  //   // })
  //   getToken(to.query.refreshToken).then(({data}) => {
  //     if (data.access_token) {
  //       localStorage.setItem('LOCAL_ACCESS_TOKEN_KEY', data.access_token)
  //       userStore.hasLogin = true
  //       router.push({
  //         path:'/',
  //       })
  //     }
  //   }).catch((e) => {
  //     message.error('登录已失效')
  //     next()
  //   }).finally(() => {
  //     // loading.close()
  //   })
  // }
  next()
})

export default router
